<?php
/**
 * user controller class
 *
 * @author raphael seebacher <raphasee@ee.ethz.ch>
 * @version 0.01
 */
class user_controller extends controller {
    protected static $instance = NULL;
    protected $user = NULL;

    protected function __construct() {

    }

    protected function __clone() {

    }

    public function get_instance() {
        if (self::$instance === NULL) {
            self::$instance = new self();
        }

        return self::$instance;
    }

    public function is_authenticated() {
        $session = session::get_instance();
        
        return ($session->get('user_id') != NULL);
    }

    public function get_user() {
        if (!$this->is_authenticated()) {
            throw new error('Authentication required.');
        }

        if ($this->user == NULL) {
            $session = session::get_instance();
            $this->user = new user($session->get('user_id'));
        }

        return $this->user;
    }

    public function get_twitter_sign_in_redirect_url() {
        $twitter_api_connection = new twitter_api_connection();
        $session = session::get_instance();

        $request_token = $twitter_api_connection->get_request_token();
        $session->set('oauth_token', $request_token['oauth_token']);
        $session->set('oauth_token_secret', $request_token['oauth_token_secret']);

        if (!$twitter_api_connection->request_successful()) {
            throw new tds_exception('Cannot get request token from Twitter.');
        }

        return $twitter_api_connection->get_authorize_url();
    }

    public function authenticate_from_twitter_callback($request) {
        $session = session::get_instance();

        if ($session->get('oauth_token') == $request->get_property('oauth_token')) {
            $twitter_api_connection = new twitter_api_connection();

            try {
                $access_token = $twitter_api_connection->get_access_token(
                  $session->get('oauth_token'),
                  $session->get('oauth_token_secret'),
                  $request->get_property('oauth_verifier')
                );

                $session->remove('oauth_token');
                $session->remove('oauth_token_secret');

                $user_credentials = new user_credentials();
                $user_credentials->set('access_token', $access_token['oauth_token']);
                $user_credentials->set('access_token_secret', $access_token['oauth_token_secret']);
                $newest_server_credentials = server_credentials::get_newest();
                $user_credentials->set('server_credentials_id', $newest_server_credentials->get('id'));
                
                $this->authenticate($user_credentials);
            }
            catch (tds_exception $e) {

            }
        }
    }

    public function authenticate($user_credentials) {
        $session = session::get_instance();

        try {
            $this->user = user::create_from_user_credentials($user_credentials);
            $session->set('user_id', $this->user->get_id());
        }
        catch (tds_exception $e) {
            $twitter_api_connection = new twitter_api_connection();
            $twitter_api_connection->set_user_credentials($user_credentials);

            try {
                $twitter_api_connection->connect_and_verify();

                $this->user = new user();
                $this->user->set(
                  'id',
                  $twitter_api_connection->get_user_id()
                );
                $this->user->set(
                  'name',
                  $twitter_api_connection->get_user_name()
                );
                $this->user->set(
                  'screen_name',
                  $twitter_api_connection->get_user_screen_name()
                );
                $this->user->save();
                $this->user->set_new_credentials($user_credentials);

                $session->set('user_id', $this->user->get_id());

                exec(
                  '../scripts/background/synchronize_db_with_twitter.php '
                  .$this->user->get_id().' > /dev/null 2>&1 &'
                );
            }
            catch (tds_exception $e) {
                throw new error('Authentication failed.');
            }
        }
    }
}
?>